﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MathLab.Messaging;

namespace MathLab.Parsing
{
    /// <summary>
    /// Parser for array creation.
    /// </summary>
    class ArrayCreateParser
    {
        private double[,] _array;
        private string _arrayName;

        public void parse(string input)
        {
            int rowNumber = 0;
            bool arrayFlag = false;
            bool nameFlag = true;

            StringBuilder name = new StringBuilder();
            StringBuilder elementString = new StringBuilder();

            List<List<double>> list = new List<List<double>>();
            list.Add(new List<double>());

            for (int i = 0; i < input.Length; i++)
            {
                string s = input[i].ToString();

                switch (s)
                {                    
                    case Literals.ARRAY_ROW_END:
                        list[rowNumber++].Add(Convert.ToDouble(elementString.ToString().Replace(".", ",")));
                        elementString = new StringBuilder();
                        if (!Literals.ARRAY_DECLARATION_END.Equals(input[i+1].ToString()))
                        {
                            list.Add(new List<double>());
                        }
                        break;
                    case Literals.ARRAY_DECLARATION_START:
                        arrayFlag = true;
                        break;
                    case Literals.ARRAY_DECLARATION_END:
                        arrayFlag = false;
                        if (!Literals.ARRAY_ROW_END.Equals(input[i-1].ToString()))
                        {
                            list[rowNumber].Add(Convert.ToDouble(elementString.ToString().Replace(".", ",")));
                        }
                        break;
                    case Literals.EQUALS:
                        nameFlag = false;
                        break;
                    case Literals.ARRAY_ELEMENT_SEPARATOR:
                        list[rowNumber].Add(Convert.ToDouble(elementString.ToString().Replace(".", ",")));
                        elementString = new StringBuilder();
                        break;
                    default:
                        if (nameFlag)
                        {
                            name.Append(s);
                        }
                        else if (arrayFlag)
                        {
                            elementString.Append(s);
                        }
                        else
                        {
                            throw new FormatException();
                        }
                        break;
                }
            }

            int longestListcount = 0;

            foreach (List<double> insideList in list)
            {
                if (insideList.Count > longestListcount)
                {
                    longestListcount = insideList.Count;
                }
            }

            arrayName = name.ToString();
            array = new double[list.Count, longestListcount];
            for (int i = 0; i < list.Count; i++)
            {
                for (int j = 0; j < longestListcount; j++)
                {
                    if (list[i].Count > j)
                    {
                        array[i, j] = list[i][j];
                    }
                    else
                    {
                        array[i, j] = 0;
                    }
                }
            }
        }

        public double[,] array
        {
            get
            {
                return _array;
            }

            set
            {
                _array = value;
            }
        }

        public string arrayName
        {
            get
            {
                return _arrayName;
            }
            set
            {
                _arrayName = value;
            }
        }
    }
}
